package com.endbd.systemmanagement.controller.dept;

import com.endbd.feignapi.model.system.Dept;
import com.endbd.systemmanagement.model.DeptCondition;
import com.endbd.systemmanagement.model.ResultBody;
import com.endbd.systemmanagement.service.dept.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/system")
@RestController
public class DeptController {

  @Autowired
  private DeptService deptService;

  //获取dept表的信息
  @GetMapping("/dept/deptList")
  public ResultBody getDeptList(){
    ResultBody resultBody = new ResultBody();
    List<Dept> jobList = deptService.getDeptList();
    if(jobList.isEmpty()) {
      resultBody.setCode(500);
      resultBody.setMessage("数据部门列表失败");
    }else{
      resultBody.setData(jobList);
      resultBody.setCode(200);
      resultBody.setMessage("success");
    }
    return resultBody;
  }

  //获取带有所属区域的部门信息
  @PostMapping("/dept/deptInfoList")
  public ResultBody getDeptInfoList(@RequestBody DeptCondition deptCondition){
    ResultBody resultBody = new ResultBody();
    List<Dept> deptList=deptService.getDeptInfoList(deptCondition);
    if(deptList.isEmpty()) {
      resultBody.setCode(500);
      resultBody.setMessage("没有相应数据");
    }else{
      resultBody.setData(deptList);
      resultBody.setCode(200);
      resultBody.setMessage("success");
    }
    return resultBody;
  }


  //插入一条新的部门信息
  @PostMapping("/dept/create")
  public ResultBody createDept(@RequestBody Dept dept){
    ResultBody resultBody = new ResultBody();
    int b=deptService.createDept(dept);
    if(b==1) {
      resultBody.setCode(200);
      resultBody.setMessage("添加新部门=>"+dept.getDeptName());
    }else if(b==2){
      resultBody.setCode(501);
      resultBody.setMessage("部门id重复");
    }else if(b==3){
      resultBody.setCode(502);
      resultBody.setMessage("部门名称重复");
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

  //注销部门
  @GetMapping("/dept/zx/{id}/{state}")
  public ResultBody zhuXiao(@PathVariable int id, @PathVariable String state){
    ResultBody resultBody = new ResultBody();
    boolean b=deptService.zhuxaio(id,state);
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("更新部门=>"+id+"的状态+=>"+(state.equals("1") ? "注销" : "启用"));
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("注销失败");
    }
    return resultBody;
  }

  @GetMapping("/dept/deptInfo/{id}")
  public ResultBody getDept(@PathVariable("id") Integer did){
    ResultBody resultBody = new ResultBody();
    Dept dept=deptService.getDept(did);
    if(dept==null){
      resultBody.setCode(500);
      resultBody.setMessage("查询失败");
    }else{
      resultBody.setData(dept);
      resultBody.setCode(200);
      resultBody.setMessage("success");
    }
    return resultBody;
  }

  @PostMapping("/dept/update")
  public ResultBody updateDept(@RequestBody Dept dept){
    ResultBody resultBody = new ResultBody();
    boolean b=deptService.updateDept(dept);
    if(b){
      resultBody.setCode(200);
      resultBody.setMessage("修改部门=>"+dept.getDeptName());
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }
}
